package cn.cq.admin.controller;

import cn.cq.annotation.OperationLogAnnotation;
import cn.cq.admin.entity.dto.BatchRemove;
import cn.cq.admin.entity.model.TResource;
import cn.cq.admin.service.TResourceService;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * cn.cq.admin.controller
 *
 * @author dicraft
 */
@RestController
@RequestMapping("/resource")
public class ResourceController {

    @Resource
    private TResourceService resourceService;

    /**
     * 新增资源
     * @param resource
     * @return
     */
    @OperationLogAnnotation(module = "资源管理模块",type = "新增",description = "新增资源")
    @SaCheckPermission(value = "Settings-Resource-Add", orRole = "SUPER-ADMIN")
    @PostMapping("/doSave")
    public SaResult doSave(@RequestBody TResource resource){
        return resourceService.doSave(resource);
    }

    /**
     * 修改资源
     * @param resource
     * @return
     */
    @OperationLogAnnotation(module = "资源管理模块",type = "修改",description = "修改资源")
    @SaCheckPermission(value = "Settings-Resource-Modify", orRole = "SUPER-ADMIN")
    @PutMapping("/doModify")
    public SaResult doModify(@RequestBody TResource resource){
        return resourceService.doModify(resource);
    }

    /**
     * 删除资源（支持单条、批量）
     * @param batchRemove
     * @return
     */
    @OperationLogAnnotation(module = "资源管理模块",type = "删除",description = "删除资源（支持单条、批量）")
    @SaCheckPermission(value = "Settings-Resource-Remove", orRole = "SUPER-ADMIN")
    @DeleteMapping("/doRemove")
    public SaResult doRemove(@RequestBody BatchRemove batchRemove){
        return resourceService.doRemove(batchRemove.getIds());
    }

    /**
     * 获取资源树
     * @return
     */
    @OperationLogAnnotation(module = "资源管理模块",type = "查询",description = "获取资源树")
    @SaCheckPermission(value = "Settings-Resource-Query", orRole = "SUPER-ADMIN")
    @GetMapping("/doList")
    public SaResult doList(){
        return resourceService.doList(false);
    }

    /**
     * 获取资源树（不含按钮）
     * @return
     */
    @OperationLogAnnotation(module = "资源管理模块",type = "查询",description = "获取资源树（不含按钮）")
    @SaCheckPermission(value = "Settings-Resource-Query", orRole = "SUPER-ADMIN")
    @GetMapping("/doParentList")
    public SaResult doParentList(){
        return resourceService.doList(true);
    }

}
